// 对象数组
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
using namespace std;

#define FILE_IN "ch09/phonebook.dat"

class PhoneList {
private:
    char name[25];
    char address[30];
    int areacode, exchange, number;

public:
    PhoneList();
    void SetName(char name[]) { strcpy(this->name, name); }
    void SetAddress(char address[]) { strcpy(this->address, address); }
    void SetPhone(int areacode, int exchange, int number);
    void ShowListing();
};

int main() {
    PhoneList myList[5];

    ifstream input;
    input.open(FILE_IN);
    if (!input) {
        cout << "输入文件不存在 " << FILE_IN << "\n";
        cout << "程序退出运行,再见\n";
        exit(1);
    }

    int areacode, exchange, number;
    char buf[35];
    for (int i = 0; i < 5; i++) {
        input.getline(buf, 25);
        myList[i].SetName(buf);

        input.getline(buf, 30);
        myList[i].SetAddress(buf);

        input.getline(buf, 4);
        areacode = atoi(buf);

        input.getline(buf, 5);
        exchange = atoi(buf);

        input.getline(buf, 7);
        number = atoi(buf);

        myList[i].SetPhone(areacode, exchange, number);
    }

    input.close();

    cout.setf(ios::left);
    cout << "你的地址簿中有下列内容\n";
    for (int i = 0; i < 5; i++) {
        myList[i].ShowListing();
    }
    cout << "再见\n";
}

PhoneList::PhoneList() {
    name[0] = '\0';
    address[1] = '\0';
    areacode = exchange = number = 0;
}

void PhoneList::SetPhone(int areacode, int exchange, int number) {
    this->areacode = areacode;
    this->exchange = exchange;
    this->number = number;
}

void PhoneList::ShowListing() {
    cout << setw(25) << name << setw(30) << address;
    cout << areacode << "-" << exchange << "-" << number << "\n";
}